package cn.bytepulse.utils;

import cn.bytepulse.web.admin.entity.SysMenu;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ThreadLocalRandom;

/**
 * 生成工具类
 *
 * @author jiejiebiezheyang
 * @since 2024-03-07 14:00
 */
public class GenerateUtils {

    private static final String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    /**
     * 生成指定长度id
     */
    public static String generateRandomNumber(int length) {
        // 使用StringBuilder拼接随机数字
        StringBuilder builder = new StringBuilder(length);
        Random random = new Random();
        for (int i = 0; i < length; i++) {
            // 生成0到9之间的随机数字
            int digit = random.nextInt(10);
            builder.append(digit);
        }
        // 将StringBuilder转换为long类型
        return builder.toString();
    }


    /**
     * 获得指定长度的随机字符串
     */
    public static String generate(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("Length must be positive");
        }
        StringBuilder sb = new StringBuilder(length);
        ThreadLocalRandom random = ThreadLocalRandom.current();
        for (int i = 0; i < length; i++) {
            int index = random.nextInt(CHARS.length());
            sb.append(CHARS.charAt(index));
        }
        return sb.toString();
    }


    /**
     * 当天yyyyMMdd0000001格式id生成
     */
    public static String generateId(String currentId) {
        String prefix = new SimpleDateFormat("yyyyMMdd").format(new Date());
        String suffix = "0000001";
        if (StringUtils.hasText(currentId)) {
            String number = currentId.substring(8);
            int i = Integer.parseInt(number) + 1;
            // 字符串格式化补零
            suffix = String.format("%07d", i);
        }
        return prefix + suffix;
    }


    /**
     * 菜单树形结构
     */
    public static List<SysMenu> menuTree(List<SysMenu> menus) {
        // 获取 一级 菜单
        List<SysMenu> pMenus = menus.stream().filter(m -> m.getParentId() == 0).toList();
        // 子菜单
        for (SysMenu pMenu : pMenus) {
            if ("D".equalsIgnoreCase(pMenu.getMenuType())) {
                int pMenuId = pMenu.getId();
                // 获取子菜单
                List<SysMenu> cMenus = menus.stream()
                        .filter(m -> m.getParentId() == pMenuId && "M".equalsIgnoreCase(m.getMenuType()))
                        .toList();
                pMenu.setChildren(cMenus);
            }
        }
        return pMenus;
    }

    /**
     * 将字节数转换为可读格式
     */
    public static String formatBytes(long bytes) {
        if (bytes < 1024) {
            return bytes + " B";
        } else if (bytes < 1024 * 1024) {
            return String.format("%.2f KB", bytes / 1024.0);
        } else if (bytes < 1024 * 1024 * 1024) {
            return String.format("%.2f MB", bytes / (1024.0 * 1024));
        } else {
            return String.format("%.2f GB", bytes / (1024.0 * 1024 * 1024));
        }
    }
}
